﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
namespace BaHuan
{
    public partial class FrmBaoCaoKeToan : Form
    {
        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();
        private ChiNhanhCongTyMgt chinhanhctyMgt = new ChiNhanhCongTyMgt();
        private KhachHangManagement khachhangMgt = new KhachHangManagement();
        private NhaCungCapManagement nhacungcapMgt = new NhaCungCapManagement();
        private NhanVienMgt nhanvienMgt = new NhanVienMgt();
        private NganHangManagement nganhangMgt = new NganHangManagement();
        private ButToanMgt buttoanMgt = new ButToanMgt();
        private PhieuChiTienManagement phieuchitienMgt = new PhieuChiTienManagement();
        private PhieuThuTienManagement phieuthutienMgt = new PhieuThuTienManagement();
        private UyNhiemChiManagement uynhiemchiMgt = new UyNhiemChiManagement();

        private ButToan buttoan = new ButToan();
        private TaiKhoanKeToan tkkt = new TaiKhoanKeToan();

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        public FrmBaoCaoKeToan()
        {
            InitializeComponent();
        }

        private void FrmBaoCaoKeToan_Load(object sender, EventArgs e)
        {
            cboNganHang.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cboNganHang.AutoCompleteSource = AutoCompleteSource.ListItems;

            cboDoiTuong.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cboDoiTuong.AutoCompleteSource = AutoCompleteSource.ListItems;

            cboMucChi.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cboMucChi.AutoCompleteSource = AutoCompleteSource.ListItems;

            cboNganHang.DataSource = GetAllNganHang();
            cboNganHang.DisplayMember = "Ten";
            cboNganHang.ValueMember = "Id";
            cboNganHang.Text = "";
        }

        public void FrmBaoCaoKeToan_formclosexemreport(bool isReOpen)
        {
            if (isReOpen)
                btnXem_Click(null, null);
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            if (txtSoHieuTaiKhoan.Text == "")
            {
                MessageBox.Show("Bạn chưa chọn tài khoản");
                return;
            }
            else
            {
                // Datetime to view report
                DateTime dtTuNgay = dtpTuNgay.Value;
                DateTime dtDenNgay = dtpDenNggay.Value;

                // Parameters to filter ButToans
                int idNganHang = 0; // Ngan Hang
                string sMucChi = ""; // Muc Chi
                string sohieutaikhoan = txtSoHieuTaiKhoan.Text; // Tai Khoan Goc
                string sTenTK = ""; // Ten Tai Khoan

                string sMaSoKhachHang = ""; // Doi Tuong..........................................................................................................................................................................................................................................................................................................................................................................................................................................................
                if (cboDoiTuong.Text != "")
                {
                    if (cboLoaiDoiTuong.SelectedIndex == 1)
                        sMaSoKhachHang = ((KhachHang)cboDoiTuong.SelectedItem).Ten;
                    else if (cboLoaiDoiTuong.SelectedIndex == 2)
                        sMaSoKhachHang = ((NhaCungCap)cboDoiTuong.SelectedItem).TenNhaCungCap;
                    else if (cboLoaiDoiTuong.SelectedIndex == 3)
                        sMaSoKhachHang = ((NhanVien)cboDoiTuong.SelectedItem).HoTen;
                    else if (cboLoaiDoiTuong.SelectedIndex == 4)
                        sMaSoKhachHang = ((ChiNhanhCongTy)cboDoiTuong.SelectedItem).TenChiNhanh;
                }

                string tkdu = txtTaiKhoanDoiUng.Text; // Tai Khoan Doi Ung
                string[] sTKDU = tkdu.Split(',');

                //sMucChi = cboMucChi.SelectedValue.ToString();
                if (cboNganHang.SelectedText != "")
                    idNganHang = Convert.ToInt32(cboNganHang.SelectedValue);

                // Filter ButToans by parameters above
                var lst = from c in GetAllButToan()
                          where c.NgayThang.Date >= dtTuNgay.Date && c.NgayThang.Date <= dtDenNgay.Date && c.TKGoc == sohieutaikhoan
                          select c;

                if (!string.IsNullOrEmpty(tkdu))
                {
                    lst = lst.Where(c => sTKDU.Contains(c.TKDoiUng));
                }

                if (!string.IsNullOrEmpty(sMaSoKhachHang))
                {
                    lst = lst.Where(c => c.MaSoKhachHang == sMaSoKhachHang);
                }

                if (!string.IsNullOrEmpty(sMucChi))
                {
                    lst = lst.Where(c => c.DienGiai.Contains(sMucChi));
                }

                if (idNganHang != 0)
                {
                    lst = from c in lst
                          join unc in GetAllUyNhiemChi() on c.SoChungTu equals unc.MaSo
                          where unc.IdNganHang == idNganHang
                          select c;                    
                }

                // Filer ButToans by parameters above but with datetime lower than starting view datetime 
                var lstTemp = from c in GetAllButToan()
                              where c.NgayThang.Date < dtTuNgay.Date && c.TKGoc == sohieutaikhoan
                              select c;

                if (!string.IsNullOrEmpty(tkdu))
                {
                    lstTemp = lstTemp.Where(c => sTKDU.Contains(c.TKDoiUng));
                }

                if (!string.IsNullOrEmpty(sMaSoKhachHang))
                {
                    lstTemp = lstTemp.Where(c => c.MaSoKhachHang == sMaSoKhachHang);
                }

                if (!string.IsNullOrEmpty(sMucChi))
                {
                    lstTemp = lstTemp.Where(c => c.DienGiai.Contains(sMucChi));
                }

                if (idNganHang != 0)
                {
                    lstTemp = from c in lstTemp
                              join unc in GetAllUyNhiemChi() on c.SoChungTu equals unc.MaSo
                              where unc.IdNganHang == idNganHang
                              select c;
                }

                TaiKhoanKeToan taikhoanketoan = (from a in GetAllTaiKhoanKeToan()
                                                 where a.MaSoTK == sohieutaikhoan
                                                 select a).FirstOrDefault();

                long SoDuBanDau = 0;
                long SDDKNo = 0;
                long SDDKCo = 0;
                long PSNo = 0;
                long PSCo = 0;
                long SDCKNo = 0;
                long SDCKCo = 0;
                long TongPhatSinhNo = 0;
                long TongPhatSinhCo = 0;

                // Sum generated in ButToans
                List<ButToan> lstButToan = lstTemp.ToList();
                List<ButToan> lstBT = lst.ToList();

                for (int i = 0; i < lstButToan.Count; i++)
                {
                    TongPhatSinhCo += lstButToan[i].GhiCo;
                    TongPhatSinhNo += lstButToan[i].GhiNo;
                }

                for (int i = 0; i < lstBT.Count; i++)
                {
                    PSNo += lstBT[i].GhiNo;
                    PSCo += lstBT[i].GhiCo;
                }

                // If statement to define type of General Ledger Accounts: Assets, Liabilities, Revenue, Expense, Hybrid
                string sFirstChar = sohieutaikhoan.Substring(0, 1);
                if (sFirstChar == "1" || sFirstChar == "2")
                {
                    // Type: Assets
                    SoDuBanDau = taikhoanketoan.SoDuBanDau;
                    long lTemp = 0;
                    lTemp = (SoDuBanDau + TongPhatSinhNo) - TongPhatSinhCo;
                    if (sohieutaikhoan.Substring(0, 3) == "131")
                    {
                        if (lTemp > 0)
                            SDDKNo = lTemp;
                        else
                            SDDKCo = 0 - lTemp;
                    }
                    else
                    {
                        SDDKNo = lTemp;
                    }

                    lTemp = (lTemp + PSNo) - PSCo;
                    if (sohieutaikhoan.Substring(0, 3) == "131")
                    {
                        if (lTemp > 0)
                            SDCKNo = lTemp;
                        else
                            SDCKCo = 0 - lTemp;
                    }
                    else
                    {
                        SDCKNo = lTemp;
                    }
                }
                else if (sFirstChar == "3" || sFirstChar == "4")
                {
                    // Type: Liabilities
                    SoDuBanDau = taikhoanketoan.SoDuBanDau;
                    long lTemp = 0;
                    lTemp = (SoDuBanDau + TongPhatSinhCo) - TongPhatSinhNo;
                    if (sohieutaikhoan.Substring(0, 3) == "331")
                    {
                        if (lTemp > 0)
                            SDDKCo = lTemp;
                        else
                            SDDKNo = 0 - lTemp;
                    }
                    else
                    {
                        SDDKCo = lTemp;
                    }

                    lTemp = (lTemp + PSCo) - PSNo;
                    if (sohieutaikhoan.Substring(0, 3) == "331")
                    {
                        if (lTemp > 0)
                            SDCKCo = lTemp;
                        else
                            SDCKNo = 0 - lTemp;
                    }
                    else
                    {
                        SDCKCo = lTemp;
                    }
                }

                if (!string.IsNullOrEmpty(lblTenTK.Text))
                    sTenTK = lblTenTK.Text;

                FrmXemReport frmXemReport = new FrmXemReport(sTenTK, this.nhanvien.HoTen, lstBT, dtTuNgay, dtDenNgay, SDDKNo, SDDKCo, PSNo, PSCo, SDCKNo, SDCKCo, sohieutaikhoan, sMaSoKhachHang);
                frmXemReport.formclosexemreport += new FormCloseXemReportEventHandler(FrmBaoCaoKeToan_formclosexemreport);
                this.Hide();
                frmXemReport.ShowDialog();
                this.Show();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cboLoaiDoiTuong_SelectedValueChanged(object sender, EventArgs e)
        {
            cboDoiTuong.DataSource = null;
            if (cboLoaiDoiTuong.SelectedIndex > -1)
            {
                int loaiDoiTuong = cboLoaiDoiTuong.SelectedIndex;
                switch (loaiDoiTuong)
                {
                    case 1:
                        cboDoiTuong.DataSource = GetAllKhachHang();
                        cboDoiTuong.DisplayMember = "Ten";
                        cboDoiTuong.ValueMember = "MaSoKhachHang";
                        break;
                    case 2:
                        cboDoiTuong.DataSource = GetAllNhaCungCap();
                        cboDoiTuong.DisplayMember = "TenNhaCungCap";
                        cboDoiTuong.ValueMember = "Id";
                        break;
                    case 3:
                        cboDoiTuong.DataSource = GetAllNhanVien();
                        cboDoiTuong.DisplayMember = "HoTen";
                        cboDoiTuong.ValueMember = "Id";
                        break;
                    case 4:
                        cboDoiTuong.DataSource = GetAllChiNhanhCongTy();
                        cboDoiTuong.DisplayMember = "TenChiNhanh";
                        cboDoiTuong.ValueMember = "Id";
                        break;
                    case 0:
                        break;
                }
                cboDoiTuong.Text = "";
            }
        }

        private void txtSoHieuTaiKhoan_Leave(object sender, EventArgs e)
        {
            TaiKhoanKeToan taikhoanketoan = (from a in GetAllTaiKhoanKeToan()
                                             where a.MaSoTK == txtSoHieuTaiKhoan.Text
                                             select a).FirstOrDefault();

            lblTenTK.Text = "";
            if (taikhoanketoan != null)
                lblTenTK.Text = taikhoanketoan.TenTK;
        }

        private void cboDoiTuong_Leave(object sender, EventArgs e)
        {
            if (cboLoaiDoiTuong.SelectedIndex != 0)
            {
                if (cboLoaiDoiTuong.SelectedIndex == 1)
                {
                    foreach (KhachHang c in cboDoiTuong.Items)
                    {
                        if (c.Ten.ToLower().Contains(cboDoiTuong.Text) && cboDoiTuong.Text!="")
                        {
                            cboDoiTuong.SelectedValue = c.Id;
                            break;
                        }
                    }
                }
                else if (cboLoaiDoiTuong.SelectedIndex == 2)
                {
                    foreach (NhaCungCap p in cboDoiTuong.Items)
                    {
                        if (p.TenNhaCungCap.ToLower().Contains(cboDoiTuong.Text) && cboDoiTuong.Text!="")
                        {
                            cboDoiTuong.SelectedValue = p.Id;
                            break;
                        }
                    }
                }
                else if (cboLoaiDoiTuong.SelectedIndex == 3)
                {
                    foreach (NhanVien emp in cboDoiTuong.Items)
                    {
                        if (emp.HoTen.ToLower().Contains(cboDoiTuong.Text) && cboDoiTuong.Text != "")
                        {
                            cboDoiTuong.SelectedValue = emp.Id;
                            break;
                        }
                    }
                }
                else if (cboLoaiDoiTuong.SelectedIndex == 4)
                {
                    foreach (ChiNhanhCongTy b in cboDoiTuong.Items)
                    {
                        if (b.TenChiNhanh.ToLower().Contains(cboDoiTuong.Text) && cboDoiTuong.Text != "")
                        {
                            cboDoiTuong.SelectedValue = b.Id;
                            break;
                        }
                    }
                }

                if (cboDoiTuong.SelectedValue == null)
                {
                    cboDoiTuong.Text = "";
                }
            }
            else
                cboDoiTuong.DataSource = null;
        }

        private void cboNganHang_Leave(object sender, EventArgs e)
        {
            foreach (NganHang b in cboNganHang.Items)
            {
                if (b.Ten.ToLower().Contains(cboNganHang.Text))
                {
                    cboNganHang.SelectedValue = b.Id;
                    break;
                }
            }

            if (cboNganHang.SelectedValue == null)
            {
                cboNganHang.Text = "";
            }
        }

        public List<TaiKhoanKeToan> GetAllTaiKhoanKeToan()
        {
            ResultObj resultObj = new ResultObj();

            List<TaiKhoanKeToan> lstTaiKhoanKeToan = new List<TaiKhoanKeToan>();

            resultObj = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            lstTaiKhoanKeToan = (List<TaiKhoanKeToan>)resultObj.Result;

            return lstTaiKhoanKeToan;
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = chinhanhctyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> listChiNhanhCongTy = (List<ChiNhanhCongTy>)resultObj.Result;

            return listChiNhanhCongTy;
        }

        public List<KhachHang> GetAllKhachHang()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = khachhangMgt.GetAllKhachHang();
            List<KhachHang> listKhachHang = (List<KhachHang>)resultObj.Result;

            return listKhachHang;
        }

        public List<NhaCungCap> GetAllNhaCungCap()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = nhacungcapMgt.GetAllNhaCungCap();
            List<NhaCungCap> listNhaCungCap = (List<NhaCungCap>)resultObj.Result;

            return listNhaCungCap;
        }

        public List<NhanVien> GetAllNhanVien()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = nhanvienMgt.GetAllNhanVien();
            List<NhanVien> listNhanVien = (List<NhanVien>)resultObj.Result;

            return listNhanVien;
        }

        public List<NganHang> GetAllNganHang()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = nganhangMgt.GetAllNganHang();
            List<NganHang> listNganHang = (List<NganHang>)resultObj.Result;

            return listNganHang;
        }

        public List<ButToan> GetAllButToan()
        {
            ResultObj resultObj = new ResultObj();
            
            resultObj = buttoanMgt.GetAllButToan();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<PhieuChiTien> GetAllPhieuChiTien()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = phieuchitienMgt.GetAllPhieuChiTien();
            List<PhieuChiTien> listPhieuChiTien = (from pc in (List<PhieuChiTien>)resultObj.Result
                                                   where pc.IsDelete == 0
                                                   select pc).ToList();
            return listPhieuChiTien;
        }

        public List<PhieuThuTien> GetAllPhieuThuTien()
        {
            ResultObj result = new ResultObj();
            result = phieuthutienMgt.GetAllPhieuThuTien();
            List<PhieuThuTien> lstPhieuThuTien = (List<PhieuThuTien>)result.Result;
            return lstPhieuThuTien;
        }

        public List<UyNhiemChi> GetAllUyNhiemChi()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = uynhiemchiMgt.GetAllUyNhiemChi();
            List<UyNhiemChi> listUyNhiemChi = (List<UyNhiemChi>)resultObj.Result;

            return listUyNhiemChi;
        }
    }
}
